[PHP] Variables loaded from a separate script are not defined within functions

Posted by Goro on Stack Overflow See other posts from Stack Overflow or by Goro
Published on 2010-04-26T20:11:58Z Indexed on 2010/04/26 20:13 UTC
Read the original article Hit count: 152

Filed under:

Hello,

I use settings.php to store general settings for my application. When I load this settings file, I can use the variables defined in settings.php in the script itself, but not within any functions I define in it.

For example, in my class definition, myclass.php:

<?php 
$preIP = dirname(__FILE__);
require_once( "preIP/settings.php" );

class MyClass {
  ...
  public function foo() {
    echo $variable_from_settings;
  }
}

The code in the function foo() will not work (the variable will not be defined).

The settings.php file looks like this:

$variable_from_settings = "bar";

Thanks,

© Stack Overflow or respective owner

Related posts about php